package com._51cto.healthy.service;

import com._51cto.healthy.base.BaseMapper;
import com._51cto.healthy.content.P;
import com._51cto.healthy.mapper.SetMealMapper;
import com._51cto.healthy.pojo.SetMeal;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/****
 * @Author:DancingHorse
 * @Description:TSetmeal业务层接口实现类
 * @Date 2022/4/14 0:16
 *****/
@DubboService
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper baseMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public BaseMapper<SetMeal> getDao() {
        return baseMapper;
    }

    @Override
    public P<SetMeal> queryPageList(String query, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //获取数据库数据
        List<SetMeal> setMeals = baseMapper.queryPageList(query);
        //把获取到的数据存入pageInfo
        PageInfo<SetMeal> pageInfo = new PageInfo<>(setMeals);
        return new P()
                .setPageNum(pageInfo.getPageNum())
                .setPageSize(pageInfo.getPageSize())
                .setPages(pageInfo.getPages())
                .setRows(pageInfo.getList())
                .setTotal(pageInfo.getTotal());
    }

    @Override
    public List<Integer> queryItemIds(Integer id) {
        return baseMapper.queryItemIds(id);
    }

    @Override
    public List<SetMeal> queryHot(Integer num) {
        //获取历史销量最高的套餐
        //t_order表
        return baseMapper.queryHot(num);
    }

    @Override
    public SetMeal queryInfoByPK(Integer id) {
        return baseMapper.queryInfoByPK(id);
    }

    //套餐搜索
    @Override
    public List<SetMeal> searchSetMeal(String k) {
        return baseMapper.searchSetMeal(k);
    }

    @Override
    public void deleteByPK(Integer pk) {
        // 获取图片名称
        SetMeal old = baseMapper.queryByPK(pk);
        String oldImgName = getImgName(old.getImg());
        int i = baseMapper.deleteByPK(pk);
        // 删除成功，将图片地址加入列表
        if (i > 0) unUseImgAdd(oldImgName);
    }

    @Override
    @Transactional
    public void insert(SetMeal setMeal) {
        //插入数据
        baseMapper.insert(setMeal);
        //获取检测组id并遍历
        setMeal.getGroupIds().forEach(groupId->{
            //插入检测组id
            baseMapper.insertGroupId(setMeal.getId(),groupId);
        });
        String imgName = getImgName(setMeal.getImg());
        unUseImgRemove(imgName);
    }

    //截取图片名称
    private String getImgName(String imgUrl){
        return imgUrl.substring(imgUrl.lastIndexOf("/")+1);
    }
    //从列表中删除图片信息
    private void unUseImgRemove(String imgName) {
        redisTemplate.boundSetOps("UNUSE_IMG").remove(imgName);
    }

    //将图片信息加入列表
    private void unUseImgAdd(String imgName) {
        redisTemplate.boundSetOps("UNUSE_IMG").add(imgName);
    }

    @Override
    @Transactional
    public void update(SetMeal setMeal) {
        // 获取修改前信息【要记得实现对应Mybatis SQL 映射】
        SetMeal old = baseMapper.queryByPK(setMeal.getId());
        // 分别获取图片修改前后的 - URL
        String oldImgName = getImgName(old.getImg());
        String newImgName = getImgName(setMeal.getImg());

        baseMapper.update(setMeal);
        //根据套餐删除检测组id
        baseMapper.deleteGroupIdBysetMeal(setMeal.getId());
        //获取检测组id并遍历
        setMeal.getGroupIds().forEach(groupId->{
            //插入检测组id和套餐
            baseMapper.insertGroupIdWithsetMeal(setMeal.getId(),groupId);
        });
        //如果旧图片 为空，或者新旧图片地址相同【无修改，无操作】
        if(!StringUtils.hasText(oldImgName)||oldImgName.equals(newImgName)){
            return;
        }
        //此时-旧图片存在，切新旧图片地址不同【符合图片修改情况】 - 旧图片加入列表
        unUseImgAdd(oldImgName);
        unUseImgRemove(newImgName);
    }

    @Override
    public SetMeal queryByPK(Integer pk) {
        return  baseMapper.queryByPK(pk);
    }
}